package com.hdl.demo.service;

import cn.com.bsfit.netty.business.BusinessService;
import cn.com.bsfit.netty.req.ReqData2MapFacade;
import cn.com.bsfit.netty.resp.BaseResponse;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 类名 DemoService
 * 描述 样例，实现接口BusinessService，返回业务逻辑返回的数据
 * 默认是返回在body对象中
 * 返回格式 { "message": {"head": {}, "body": {}} }
 * 该这个标签格式可自定义实现
 *
 * @author hedonglin
 * @version 1.0
 * @date 2020/4/27 9:48
 */
@Service
public class DemoService implements BusinessService {
    @Override
    public Map<String, Object> reqHandler(Map<String, Object> reqMap, ReqData2MapFacade reqData2MapFacade) throws Exception {
        Map<String, Object> data = Maps.newHashMap();
        data.put("name", "hedonglin");

        // 获取facade中的数据
        boolean b = reqData2MapFacade.isJson();
        data.put("isJson", b);
        return BaseResponse.builder().success(data);
    }

    /**
     * 自定义异常返回结果（可重写，不重写返回默认错误数据结构BaseResponse#error方法）
     * @param errMsg 错误信息
     * @return 统一异常返回结果
     */
    @Override
    public Map<String, Object> getBaseResponseError(String errMsg) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("code", "500");
        result.put("errMsg", errMsg);
        return result;
    }
}
